package org.happen.android.widget;

import org.happen.android.app.AppContext;
import org.liushui.mycommons.android.log.McLog;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.PopupWindow;
import android.widget.PopupWindow.OnDismissListener;

import org.happen.android.R;

public class PopupDialog
{

	View target;
	Activity activity;
	boolean useBackground;

	PopupWindow window;
	BasePopupDialogWidget widget;

	public PopupDialog(View target, Activity context)
	{
		this(target, context, true);
	}

	public PopupDialog(View target, Activity context, boolean useBg)
	{
		this.target = target;
		this.activity = context;
		useBackground = useBg;
	}

	/**
	 * 设置内容布局
	 * 
	 * @ReqParam widget
	 */
	public void setContentView(BasePopupDialogWidget widget)
	{
		int[] size = AppContext.getInstance().getScreenSize();
		setContentView(widget, size[0] / 2, size[1] * 2 / 3);
	}

	/**
	 * 设置内容布局
	 * 
	 * @ReqParam widget
	 * @ReqParam w
	 * @ReqParam h
	 */
	public void setContentView(BasePopupDialogWidget widget, int w, int h)
	{
		this.widget = widget;
		ViewGroup vg = null;
		if (useBackground)
		{
			vg = (ViewGroup) View.inflate(activity, R.layout.popup_dialog, null);
			vg.addView(widget);
		} else
		{
			vg = widget;
		}
		widget.setPopupDialog(this);
		window = new PopupWindow(vg, w, h, true);
		window.setOnDismissListener(new OnDismissListener() {
			public void onDismiss()
			{
				dismiss();
			}
		});
		window.setAnimationStyle(android.R.style.Animation_Dialog);
	}

	public void dismiss()
	{
		McLog.m(this, "dismiss");
		if (window != null)
		{
			widget.onDismiss();
			window.dismiss();
		}
	}

	public boolean isShow()
	{
		if (window == null)
		{
			return false;
		}
		return window.isShowing();
	}

	public void show()
	{
		show(Gravity.CENTER, 0, 0);
	}

	public void show(int gravity, int x, int y)
	{
		McLog.m(this, "show");
		if (!activity.isFinishing())
		{
			widget.onShow();
			window.showAtLocation(target, gravity, x, y);
		}
	}

	public void showAsDropDown(View view)
	{

		window.showAsDropDown(view);
	}

	/**
	 * 设置是否在外面点击就消失对话框
	 * 
	 * @ReqParam isDismiss
	 */
	public void setDismissOnTouchOuter(boolean isDismiss)
	{
		if (isDismiss)
		{
			window.setBackgroundDrawable(new ColorDrawable());
		} else
		{
			window.setBackgroundDrawable(null);
		}
	}

	public PopupWindow getPopupWindow()
	{
		return window;
	}

	public interface IPopupDialogWidget
	{
		public void onShow();

		public void onDismiss();
	}

	public static class BasePopupDialogWidget extends FrameLayout implements IPopupDialogWidget
	{

		protected Context context;
		protected PopupDialog popupDialog;

		public BasePopupDialogWidget(Context context)
		{
			super(context);
			this.context = context;
		}

		public void init()
		{
		}

		public void onShow()
		{

		}

		public void onDismiss()
		{

		}

		public void setOnPopupDialogWidgetCallback(OnPopupDialogWidgetCallback callback)
		{

		}

		public PopupDialog getPopupDialog()
		{
			return popupDialog;
		}

		public void setPopupDialog(PopupDialog popupDialog)
		{
			this.popupDialog = popupDialog;
		}
	}

	public interface OnPopupDialogWidgetCallback
	{

	}
}